<script setup>
import {reactive,computed} from 'vue'
import {useRouter} from 'vue-router';
const router = useRouter()
import {ref, onMounted} from 'vue'
import Choose from '../../components/Choose.vue'
import Skeleton from '../../components/Skeleton.vue'
import RadioGroup from '../../components/RadioGroup.vue'
import NoResult from '../../components/NoResult.vue'
import Declaration from '../../components/Declaration.vue'
import chartPie from './chartPie.vue'
import {getThunder} from '../../api/index.js'
import {sleep} from '../../utils/util.js'
const usePage = ()=>{
  const skeleton = ref(true)
  const listData = reactive({
    danger: [],
    vigilant: [],
    notice: []
  });
  const displayData = computed(()=>{
    const selectKey = selectedOption.value;
    const data = listData[selectKey] || [];
    if(onlySeeSelf.value){
      // return  []
      return data.slice(0,2);
    }else{
      return data;
    }
  })

  const onClickLeft = () => history.back();
  const onlySeeSelf = ref(false);
  const selectedOption = ref('danger');
  const onRadioChange = (value) => {
    console.log('Selected option changed to:', value);
    selectedOption.value = value
  }
  const goResult = ()=>{
    router.push('/result')
  }
  onMounted(async () => {
    getThunder().then(async data=>{
      // console.log('data',data)
      const {danger,vigilant,notice} = data;
      listData.danger = danger;
      listData.vigilant = vigilant;
      listData.notice = notice;
      await sleep(700)
      skeleton.value = false;
    })
  })

  return {
    skeleton,
    displayData,
    selectedOption,
    onClickLeft,
    onRadioChange,
    onlySeeSelf,
    goResult
  }
}
const {
  skeleton,
  displayData,
  selectedOption,
  onClickLeft,
  onRadioChange,
  onlySeeSelf,
  goResult
} = usePage();

</script>

<template>
  <Skeleton v-if="skeleton" :row="15" pos="center"/>
  <div v-else class="app-container">
    <van-sticky>
      <van-nav-bar
          title="个股扫雷首页"
          left-arrow @click-left="onClickLeft"
      >
        <template #left>
          <img src="@/assets/images/icon_btn_back_black@3x.png" width="16" height="16" alt="">
        </template>
      </van-nav-bar>
    </van-sticky>
    <div class="search">
      <div class="search__choose">
        <Choose></Choose>
      </div>
    </div>
    <chartPie/>
    <div class="filter">
      <div class="filter-switch">
        <RadioGroup
            :options="[
              { value: 'danger', label: '危险' },
              { value: 'vigilant', label: '警惕' },
              { value: 'notice', label: '注意' },
            ]"
            :model="selectedOption"
            @change="onRadioChange"
        ></RadioGroup>
        <div class="switch">
          <span class="switch__label">仅看自选</span>
          <van-switch v-model="onlySeeSelf" active-color="rgba(247, 51, 45, 1)" inactive-color="#dcdee0"/>
        </div>
      </div>
      <div class="divide clearfix"></div>
      <div class="record">
        <div v-if="displayData.length"
             class="record__item"
             v-for="item in displayData"
             @click="goResult"
        >
          <div class="status">
            <div class="mainTxt">{{item.status}}</div>
            <div class="subTxt">{{item.count}}项风险</div>
          </div>
          <div class="description">
            <div class="mainTxt">
              <span>{{item.name}}</span>
              <span>SH000733</span>
            </div>
            <div class="subTxt">
              公告风险，公募减持，限售解禁，高管异常变动，信息披露造假，诉讼风险，交易所问询，质押风险
            </div>
          </div>
        </div>
        <NoResult v-else/>
      </div>
    </div>
  </div>
  <Declaration/>
</template>

<style scoped lang="scss">
.app-container {
  background: #f5f6fa;
  //min-height: 100vh;
  min-height: calc(100vh - 220px);

  .search {
    .search__choose {
      background: #fff;
      border-radius: 8px;
      margin: 16px;

      .choose-line {
        border-top: 1px dashed rgb(165, 166, 173);
        margin: -3px auto 0;
        width: 240px;
      }

      .choose-bottom {
        width: 100%;
        padding: 8px 0 8px 0;
        display: flex;
        justify-content: center;

        img {
          width: 15px;
          height: 16px;
        }

        .choose-txt {
          padding: 0 10px;
          height: 14px;
          text-align: right;
          color: rgba(108, 110, 122, 1);
          font-family: PingFangSC-Regular;
          font-weight: 400;
          font-size: 13px;
          line-height: 14px;
        }
      }
    }

    .search__message {
      //border:1px solid red;
      margin: 16px 16px 0;
      height: 34px;
      display: flex;
      justify-content: center;
      align-items: center;
      mix-blend-mode: normal;
      color: rgba(108, 110, 122, 1);
      font-family: PingFangSC-Regular;
      font-weight: 400;
      font-size: 13px;
      line-height: 18px;
      border-radius: 8px;
      background: rgba(239, 242, 255, 1);

      .text-1 {
        color: rgba(247, 51, 45, 1);
      }

      .text-2 {
        color: rgba(0, 47, 167, 1);
      }

      .icon-message {
        height: 16px;
        width: 16px;
        margin-right: 10px;
      }

      .icon-arrow {
        margin-left: 10px;
      }
    }

    .search__submit {
      margin: 8px auto 0;
      display: flex;
      align-items: center;
      justify-content: center;
      width: 343px;
      height: 49px;
      border-radius: 24.5px;
      background: rgb(232, 72, 56);

      &:active {
        //opacity: 0.9;
      }

      .search__submit__btn {
        color: rgba(246, 246, 246, 1);
        font-family: PingFangSC-Medium;
        font-weight: 450;
        font-size: 18px;
      }
    }
  }
}

.filter {
  margin-top: 12px;
  overflow: hidden;
  background: #fff;

  .filter-switch {
    display: flex;
    justify-content: space-between;
    padding: 13px 16px 0;

    .switch {
      display: flex;
      align-items: center;

      .van-switch {
        font-size: 14px;
      }

      .switch__label {
        margin-right: 8px;
        height: 20px;
        text-align: center;
        color: rgba(108, 110, 122, 1);
        font-family: PingFangSC-Regular;
        font-weight: 400;
        font-size: 14px;
        line-height: 20px;
      }
    }
  }

}

.divide {
  width: 100%;
  height: 16px;
  background: red;
  mix-blend-mode: normal;
  background: linear-gradient(180deg, rgba(255, 255, 255, 1) 0%, rgba(245, 246, 250, 1) 100%);
}
.record {
  background: #fff;
  padding: 6px 16px 0;
  width: 100%;
  border-radius: 16px 16px 0 0;
  background: rgba(255, 255, 255, 1);

  .record__item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 70px;
    border-bottom: 1px solid #F6F7F7;

    .status {
      //border:1px solid red;
      width: 100px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;

      .mainTxt {
        font-size: 14px;
      }

      .subTxt {
        font-size: 12px;
        color: #ccc;
      }
    }

    .description {
      margin-left: 12px;

      .mainTxt {
        font-size: 14px;
      }

      .subTxt {
        font-size: 12px;
        color: #ccc;
      }
    }
  }
}
</style>